<?php
/**
 * Created by PhpStorm.
 * User: TS
 * Date: 2017/4/4
 * Time: 20:26
 */
namespace core\http;

class Request {

    protected static $instance;

    /**
     * @var string 模块 控制器  方法
     */
    protected $module;
    protected $controller;
    protected $action;

    protected $filter = [];

    protected $request;

    public function __construct(\swoole\http\request $request)
    {
        $this->request = $request;
    }

    /**
     * @param \swoole\http\request $request
     * @return static
     */
    public static function getInstance(\swoole\http\request $request = null)
    {
        if (!isset(self::$instance)) {
            self::$instance = new static($request);
        }
        return self::$instance;
    }

    protected function filter($item, $key, $filter)
    {
        if (!empty($filter)) foreach ($filter as $f ) {
            if (is_callable($f)) $item = $f($item);
        }
    }

    /**
     * 获取参数值
     * @param $name
     * @param null $default
     * @param array $filter
     * @return array|mixed|null
     */
    public function get($name, $default = null, $filter = [])
    {
        $params = explode('.', $name);
        if (count($params) <= 1) {
            $method = 'get';
            $key = current($params);
        } else {
            list($method, $key) = $params;
        }
        if (is_string($filter)) $filter = explode(',', $filter);
        $filter = array_merge($this->filter, $filter);

        if (property_exists($this->request, $method)) {
            $return = !empty($key) ? (isset($this->request->$method[$key]) ? $this->request->$method[$key] : $default) : $this->request->$method;
        } else {
            $return = null;
        }

        if (is_array($return)) {
            array_walk_recursive($return, array($this, 'filter'), $filter);
        } else {
            $return = (array)$return;
            array_walk($return, [$this, 'filter'], $filter);
            reset($return);
            $return = current($return);

        }
        return $return;
    }

    /**
     * 设置参数值
     * @param $name
     * @param $value
     * @return mixed
     */
    public function set($name, $value)
    {
        $params = explode(',', $name);
        if (count($params) <= 1) {
            $method = 'get';
            $key = current($params);
        } else {
            list($method, $key) = $params;
        }
        return $this->request->$method[$key] = $value;
    }

    /**
     * 获取当前请求的时间
     * @access public
     * @param bool $float 是否使用浮点类型
     * @return integer|float
     */
    public function time($float = false)
    {
        return $float ? $_SERVER['REQUEST_TIME_FLOAT'] : $_SERVER['REQUEST_TIME'];
    }

    /**
     * 设置控制器
     * @param string $name
     * @return string
     */
    public function controller($name = '')
    {
        if (!empty($name)) $this->controller = $name;
        return $this->controller;
    }

    /**
     * 设置模块
     * @param string $name
     * @return string
     */
    public function module($name = '')
    {
        if (!empty($name)) $this->module = $name;
        return $this->module;
    }

    /**
     * 设置方法
     * @param string $name
     * @return string
     */
    public function action($name = '')
    {
        if (!empty($name)) $this->action = $name;
        return $this->action;
    }

    /**
     * 是否为GET请求
     * @access public
     * @return bool
     */
    public static function isGet()
    {
        return self::method() == 'GET';
    }

    /**
     * 是否为POST请求
     * @access public
     * @return bool
     */
    public static function isPost()
    {
        return self::method() == 'POST';
    }

    /**
     * 是否为PUT请求
     * @access public
     * @return bool
     */
    public static function isPut()
    {
        return self::method() == 'PUT';
    }

    /**
     * 是否为DELETE请求
     * @access public
     * @return bool
     */
    public static function isDelete()
    {
        return self::method() == 'DELETE';
    }

    /**
     * 是否为HEAD请求
     * @access public
     * @return bool
     */
    public static function isHead()
    {
        return self::method() == 'HEAD';
    }

    /**
     * 是否为PATCH请求
     * @access public
     * @return bool
     */
    public static function isPatch()
    {
        return self::method() == 'PATCH';
    }

    /**
     * 是否为OPTIONS请求
     * @access public
     * @return bool
     */
    public static function isOptions()
    {
        return self::method() == 'OPTIONS';
    }

    /**
     * 是否为cli
     * @access public
     * @return bool
     */
    public static function isCli()
    {
        return PHP_SAPI == 'cli';
    }

    /**
     * 是否为cgi
     * @access public
     * @return bool
     */
    public static function isCgi()
    {
        return strpos(PHP_SAPI, 'cgi') === 0;
    }

    /**
     * 当前的请求类型
     * @return string
     */
    public static function method()
    {
        return  strtoupper(isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']) ?
            $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] :
            (isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'));
    }

    /**
     * 是否为ajax 请求
     * @return bool
     */
    public static function isAjax()
    {
        return isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"])=="xmlhttprequest";
    }

    public function __get($name)
    {
        return $this->request->$name;
    }

    public function __set($name, $value)
    {
        return $this->request->$name = $value;
    }

}